package edu.sias.hyznxfapi.checkCompliance.checkPicture.utils.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import edu.sias.hyznxfapi.checkCompliance.checkAudioFre.Constants;
import edu.sias.hyznxfapi.checkCompliance.checkAudioFre.utils.MyUtil;

import java.util.Map;

public class CheckPicture {
    private static String url = "https://audit.iflyaisol.com//audit/v2/image";

    private static final String APPID = Constants.APPID;
    private static final String APISecret = Constants.APISecret;
    private static final String APIKey = Constants.APIKey;


//    public static String modeType = "base64"; // 本地文件选这个
    public static String modeType = "link"; // 阿里文件选这个 下面json也是两两和这个对应
    public static String PictureCheck(String imageUrl) throws Exception {
        String json = "{\n" +
//                "  \"content\": \"" + Base64.getEncoder().encodeToString(MyUtil.imageToByteArray(imageUrl)) + "\"\n" +
                "  \"content\": \"" + imageUrl + "\"\n" +
//                "  \"content\": \"" + "https://edu-subject-1.oss-cn-beijing.aliyuncs.com/2024/05/20/X%7D5%7DH%7B%40R%24H%6000%29T%60%7EY8I%24%7DY.jpg" + "\"\n" +
                "}";
        Map<String, String> urlParams = MyUtil.getAuth(APPID, APIKey, APISecret);
        String returnResult = MyUtil.doPostJson(url, urlParams, json);
        JSONObject jsonObject = JSON.parseObject(returnResult);

        JSONObject dataObject = jsonObject.getJSONObject("data");
        JSONObject resultObject = dataObject.getJSONObject("result");
        String suggest = resultObject.getString("suggest");
        String categoryDescription = "";
        // 确保detail是一个对象，并且包含category_list数组
        if (resultObject.containsKey("detail")) {
            JSONObject detailObject = resultObject.getJSONObject("detail");
            if (detailObject.containsKey("category_list")) {
                JSONArray categoryList = detailObject.getJSONArray("category_list");
                if (categoryList.size() > 0) {
                    // 获取数组中的第一个元素
                    JSONObject firstCategory = categoryList.getJSONObject(0);
                    categoryDescription = firstCategory.getString("category_description");
                    System.out.println("第一个category_description的值: " + categoryDescription);
                }
            }
        }

        System.out.println(suggest); // pass 通过 block 不合规内容
        if (suggest.equals("block")){
            return categoryDescription;
        }
        return suggest;
    }

    public static void main(String[] args) {
        try {
            new CheckPicture().PictureCheck("https://team-ckw.oss-cn-wuhan-lr.aliyuncs.com/avatar.150x150%20%281%29.jpg");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
